/**
 * 
 */
package f.h.dragon.util;

import static f.h.dragon.util.Arrays.swap;

import java.io.File;
import java.util.Arrays;

/**
 * RandomUtil: 随机数相关工具类
 *
 * @author Wenlong Meng(wenlong.meng@gmail.com)
 * @version 1.0 at 2012/02/08
 * @since 1.0
 */
public class RandomUtil {
	
	//Logic
	/**
	 * 生成count个介于min(包含)和max(不包含)之间不重复的随机数
	 * 
	 * @param min - the min of random
	 * @param max - the max of random
	 * @param count - the count of random
	 * @return
	 */
	public static int[] randoms(int min, int max, int count){
		//range check
		if(min < 0 || max < min || count < 1 || count > max - min){
			throw new IllegalArgumentException("min=" + min + ", max=" + max + ", count=" + count);
		}
		
		//init randoms
		int length = max - min;
		int[] randoms = new int[length];
		for(int i = 0, counti = length; i < counti; i++){
			randoms[i] = min + i;
		}
		
		//make randoms
		java.util.Random random = new java.util.Random();
		for(int i = 0; i < count; i++){
			swap(randoms, i, random.nextInt(length));
		}
		
		return Arrays.copyOf(randoms, count);
	}
	
	/**
	 * 生成count个介于min(包含)和max(不包含)之间不重复的的随机数，并存储在file中
	 * 
	 * @param min - the min of random
	 * @param max - the max of random
	 * @param count - the count of random
	 * @return
	 */
	public static void randoms(int min, int max, int count, File file){
		//range check
		if(min < 0 || max < min || count < 1 || count > max - min){
			throw new IllegalArgumentException("min=" + min + ", max=" + max + ", count=" + count);
		}
		
		//init randoms
		int length = max - min;
		int[] randoms = new int[length];
		for(int i = 0, counti = length; i < counti; i++){
			randoms[i] = min + i;
		}
		
		//make randoms
		java.util.Random random = new java.util.Random();
		for(int i = 0; i < count; i++){
			swap(randoms, i, random.nextInt(length));
		}
		
	}

}
